home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_100 / 162_01 / ftoa.c < prev    next >
Text File  |  1985-08-19  |  3KB  |  173 lines

  1.     /*    file ftoa.c    */
  2.     /*    convert float type to ASCII string    */
  3. #asm
  4.     NAME    ('FTOA')
  5. #endasm
  6.  
  7. #define    BUFL    40
  8. static    char    *pc;
  9. static    int    m[12],chinc,n,stz,d;
  10. static    int    *pm3,*pm2, *pm1, *pm0 = &m[0];
  11. static    int    *rndbak();
  12. extern    float    fmten(),fdten();
  13.  
  14. char    *
  15. ftoa(fmt,digits,fl,where)
  16. char    fmt;
  17. int    digits;
  18. float    fl;
  19. char    where[];
  20. {
  21.  
  22.     static    char    c;
  23.     static    int    declim,expint,moc,expd,a;
  24.     static    float    fn,fz;
  25.  
  26.     pc = &where[0];
  27.     fn = fl;
  28.     declim = digits;
  29.     d = fmt;
  30.     chinc = 0;
  31.  
  32.     if(fn == fz) {
  33.         putk('0');
  34.     }
  35.     else {
  36.     moc = stz = 0;
  37.     if(d == 'f' || d == 'F')
  38.         moc = 1;    /* mode of conversion is Float */
  39.     if(d <= 'F')
  40.         stz = 1;    /* suppress trailing zero's */
  41.  
  42.     if(fn < 0) {
  43.         putk('-');
  44.         fn = -fn;
  45.     }
  46.  
  47.     expint = 0;
  48.     if(fn > 9) { /* pos expnt, not decimal normal */
  49.         while(expint < 18) {
  50.             if(fn < 10)
  51.                 break;
  52.             fn = fdten(fn);
  53.             ++expint;
  54.         }
  55.     }
  56.     else if(fn < 1) {  /* neg exponent */
  57.         while(expint > -20) {
  58.             if(fn >= 1)
  59.                 break;
  60.             fn = fmten(fn);
  61.             --expint;
  62.         }
  63.     }
  64.     else;    /* fn is decimal normal */
  65.  
  66.     expd = expint;
  67.     if(moc == 0)    /* exponential mode */
  68.         expint = 0;
  69.     a = 7;
  70.     if((n = (declim + expint + 2)) < a && n > 1)
  71.         a = n;
  72.  
  73.     *pm0 = '0';
  74.     for(pm1 = pm0 +1; pm1 <= pm0 +(a+2);){ /* mantissa to ASCII */
  75.         *pm1++ =((d = fn) + '0');
  76.         fn = fmten(fn - d);
  77.     }
  78.  
  79.     while(pm1 >= pm0 + a) {        /* round back */
  80.         pm3 = pm1 = rndbak(pm1,'5','5');
  81.         while((pm2 =rndbak(pm3,'9','0')) >= pm0 && pm2 != pm3)
  82.             pm3 = pm2;
  83.     }
  84.  
  85.     pm1 = pm0;
  86.     if(*pm0 != '0') {
  87.         ++expd;
  88.         if(moc == 1)    /* float format */
  89.             ++expint;
  90.     }
  91.     else
  92.         ++pm1;
  93.     for(d = 0; chinc < BUFL; ++d, --expint) {
  94.         if(expint >= 0) {
  95.             d = 0;
  96.             putn();
  97.         }
  98.         else {
  99.             if(d == 1)
  100.                 putk('.');
  101.             if((a = (d + expd)) < 0 && moc == 1) {
  102.                 putk('0');    /* leading zero */
  103.             }
  104.             else if((n = putn()) == 1)
  105.                 break;
  106.             else;
  107.         }
  108.         if(d >= declim)
  109.             break;
  110.     }
  111.     if(moc == 0) {
  112.         putk('e');
  113.         if(expd < 0) {
  114.             putk('-');
  115.             expd = -expd;
  116.         }
  117. expd;
  118. #asm
  119.     XCHG
  120.     LXI    H,10
  121.     CALL    c.div##
  122.     PUSH    D
  123.     LXI    B,48
  124.     DAD    B
  125.     PUSH    H
  126.     CALL    putk
  127.     POP    H
  128.     POP    H
  129.     DAD    B
  130.     PUSH    H
  131.     CALL    putk
  132.     POP    H
  133. #endasm
  134.     }
  135.     }
  136.     putk('\0');
  137.     return(--pc);
  138. }
  139.  
  140. putk(c)
  141. int    c;
  142. {
  143.     if(chinc++ < BUFL)
  144.         *pc++ = c;
  145. }
  146.  
  147. static    int    *rndbak(ip,hilev,lolev)
  148. int    *ip, hilev, lolev;
  149. {
  150.     static    int    *ips;
  151.     ips = ip;
  152.     if(*ips > hilev) {
  153.         *ips-- = '\0';
  154.         ++(*ips);
  155.     }
  156.     else if(*ips <= lolev)
  157.         *ips-- = '\0';
  158.     else;
  159.     return(ips);
  160. }
  161.  
  162. putn()
  163. {
  164.     if(*pm1 != '\0')
  165.         putk(*pm1++);    /* significant digit */
  166.     else if(stz == 0 || d == 0) {
  167.         putk('0');    /* trailing zero */
  168.         return(0);
  169.     }
  170.     else
  171.         return(1);
  172. }
  173.